package com.ssh.service.impl;
import com.common.tool.Page;
import com.ssh.entity.Consumer;
import com.ssh.entity.User;
import com.common.repostitory.ConsumerRepository;
import com.common.repostitory.CommonRepository;
import com.ssh.service.ConsumerService;
import com.common.tool.Message;
import com.common.tool.PagingTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zpen on 2018/2/24.
 */

@Service
@Transactional
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    ConsumerRepository personRepository;

    @Autowired
    CommonRepository commonRepository;



    public Long  saveConsumer( Consumer consumer) {
        consumer.setStatus("10");
       consumer.setCreated(new Date());
        Long count = commonRepository.save(consumer);
        User user = new User();
        user.setUsername(consumer.getUsername());
        user.setUserType(2L);
        user.setPassword("123456");
        user.setConId(count);
        user.setState(1);

        Long userId = commonRepository.save(user);
         if(userId>0&&count>0){
             return count;
         }else {
             return 0L;
         }
    }

    public Consumer getConsumerByProperty( String property,Object value) {
        return  commonRepository.getEntityByProperty(Consumer.class,property,value);
    }

    public Consumer getConsumerById(Long id) {
        return (Consumer) commonRepository.load(Consumer.class,id);
    }

    public List<Consumer> getConsumer(Consumer consumer) {
        List<Consumer> list = new ArrayList();
        list.add(personRepository.getConsumer(consumer));
        return list;
    }

    public PagingTool queryForPage(Consumer consumer, Page pages) {
        PagingTool page = new PagingTool();
        int allRow = personRepository.getAllRowCount(consumer);
        List<Map<String,Object>> list = personRepository.queryForPage(consumer,pages);
        page.setPageNo(Integer.valueOf(pages.getOffset()));
        page.setPageSize(Integer.valueOf(pages.getLimit()));
        page.setTotal(allRow);
        page.setRows(list);
        return page;
    }

    public Message delConsumer(String id, String mode) {
        Message msg = new Message();
        Long id_1 =Long.valueOf(id);
        try{
            Consumer consumer = commonRepository.getEntity(Consumer.class,id_1);
            User user =  commonRepository.getEntityByProperty(User.class,"conId",id_1);
            Integer repairNumber =  commonRepository.queryForObject("select count(*) from repair where con_id=? ",new Object[]{id_1});
            if(repairNumber>0){
                msg.setSuccess("repairIsNoNot");
            }else {
                if ("direct".equals(mode)) {
                    commonRepository.deleteById(Consumer.class, id_1);
                    if(user!=null){
                        commonRepository.deleteById(User.class, id_1);
                    }
                } else {
                    consumer.setStatus("90");
                    commonRepository.update(consumer);
                    if(user!=null) {
                        user.setState(0);
                        user.setChangeTime(new Date());
                        commonRepository.update(user);
                    }
                }
                msg.setSuccess("success");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setSuccess("exception");
        }


        return msg;
    }


}
